package com.sgcc.pda.mdrh.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.support.multidex.MultiDex;

import com.cepri.service.CepriServiceManager;
import com.qihoo360.replugin.RePlugin;
import com.qihoo360.replugin.RePluginConfig;
import com.sgcc.pda.baselibrary.application.HZBaseApplication;
import com.sgcc.pda.baselibrary.thread.FixedThreadUtil;
import com.sgcc.pda.baselibrary.utils.LogUtil;
import com.sgcc.pda.hzaidl.AIDLServiceImpl;
import com.sgcc.pda.mdrh.h5.provider.DataProviderUtil;
import com.sgcc.pda.mdrh.task.safelibrary.BuildConfig;
import com.sgcc.pda.sdk.utils.db.SharepreferenceUtil;
import com.sgcc.pda.ssal.mqtt.proxy.client.MqttProxyClient;
import com.sgcc.pda.ssal.mqtt.proxy.server.MqttProxyServer;

/**
 * 描述：
 * 作者：jianiuqi 2020-09-15 11:23
 * 修改描述：
 * 修改人：XXX 2020-09-15 11:23
 * 修改版本：
 */
public class ZoomApplication extends HZBaseApplication {

    public Context mContent;
    public static Activity mTopActivity;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);

        RePluginConfig c = createConfig();
        // Replugin插件配置
        RePlugin.App.attachBaseContext(this, c);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContent = this;
        // Replugin插件配置
        RePlugin.App.onCreate();
        registerActivityLifecycleCallbacks(callbacks);
        initService();
    }

    /**
     * 子类可以复写此方法来自定义RePluginConfig。请参见 RePluginConfig 的说明
     *
     * @return 新的RePluginConfig对象
     * @see RePluginConfig
     */
    protected RePluginConfig createConfig() {
        RePluginConfig config = new RePluginConfig();
        // 开启插件使用宿主的类
        config.setUseHostClassIfNotFound(true);
        config.setMoveFileWhenInstalling(false);
//        RePlugin.addCertSignature("");
        // 是否开启签名校验
//        config.setVerifySign(!BuildConfig.DEBUG);
        return config;
    }

    /**
     * 绑定AIDL服务
     */
    protected void initService() {
        AIDLServiceImpl.getInstance()
                .bindService(
                        mContent,
                        "com.sgcc.pda.safework");

        CepriServiceManager.getInstance().bind(mContent);
        //初始化ShareperferenceInfo类/此类主要用于给业务App暴露和提供数据服务公共数据
        DataProviderUtil.getInstance().init(mContent);
        if (!isRemoteService()) {
            //启动本地mqtt代理
            startProxyServer();
        }
    }

    /**
     * 启动本地MQTT代理服务器
     */
    public static void startProxyServer() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (BuildConfig.ISSSAL) {
                    MqttProxyServer.startServer();
                }
            }
        };
        FixedThreadUtil.executeThread(runnable);
    }

    /**
     * 启动本地MQTT代理客户端
     */
    public static void startProxyClient() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                //ip port需要兼容usb
                String ip = SharepreferenceUtil.getCommIp(getInstance());
                String ports = SharepreferenceUtil.getCommPort(getInstance());
                int port = Integer.parseInt(ports);
                LogUtil.e("mqtt", "IP=" + ip + " port=" + port);
                MqttProxyClient.connect(ip, port);
            }
        };
        FixedThreadUtil.executeThread(runnable);
    }

    /**
     * 获取当前栈定Activity
     */
    Application.ActivityLifecycleCallbacks callbacks = new Application.ActivityLifecycleCallbacks() {
        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {

        }

        @Override
        public void onActivityResumed(Activity activity) {
            mTopActivity = activity;
            LogUtil.i("当前activity类名:", activity.getLocalClassName());
        }

        @Override
        public void onActivityPaused(Activity activity) {
            mTopActivity = null;
        }

        @Override
        public void onActivityStopped(Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    };
}
